# _*_coding=utf-8 _*_
"""
@author :   xh
@date   :   2023/11/1 09:57
"""
from mathsolver.SyntacticParser.parser import auto_parser
from mathsolver.init_engine import engine
from mathsolver.functions.fushu.FuShu_JieFangCheng import FSSolveEq
from mathsolver.functions.fushu.FuShu_Property import FSConjugate
from mathsolver.functions.fushu.FuShu_BiaoZhunHua import FSNormalized
from mathsolver.functions.fushu.FuShu_getTemplateInput import get_expression_ahfs0001, get_expression_ahfs0002
from utils.config import Config

conf = Config(config_file='conf/config.json')
config = conf.config
syntacticParserUrl = config[conf.env]["syntacticParserUrl"]


def func(x):
    return x.replace(",", "，")


def fs_template_solve_engine(raw):
    template_id = raw.get('template_id', '')
    flag = 0
    question = None
    if template_id == 'AHFS0001':
        args = get_expression_ahfs0001(raw)
        poly = args[0].latex
        require_answers = raw['params']['require_answers']
        if require_answers == ['value']:
            question = "求" + poly + "的值"
        elif require_answers == ['real']:
            question = "求" + poly + "的实部"
        elif require_answers == ['imaginary']:
            question = "求" + poly + "的虚部"
        elif require_answers == ['conjugate']:
            question = "求" + poly + "的共轭复数"
        elif require_answers == ['real+imaginary']:
            question = "求" + poly + "的实部与虚部之和"
        elif require_answers == ['modulus']:
            question = "求" + poly + "的模"
        elif require_answers == ['quadrant']:
            question = "求" + poly + "对应的点位于第( )象限"
        elif require_answers == ['coordinate']:
            question = "求" + poly + "对应的点坐标"
        else:
            flag = -1
    elif template_id == 'AHFS0002':
        args = get_expression_ahfs0002(raw)
        eq = args[0].latex
        require_answers = raw['params']['require_answers']
        answer_definitions = raw['params']['answer_definitions']
        if len(answer_definitions) == 1:
            answer_definition = answer_definitions[0]
            if require_answers == ['value']:
                question = "已知" + eq + "," + "求" + answer_definition + "的值"
            elif require_answers == ['real']:
                question = "已知" + eq + "," + "求" + answer_definition + "的实部"
            elif require_answers == ['imaginary']:
                question = "已知" + eq + "," + "求" + answer_definition + "的虚部"
            elif require_answers == ['conjugate']:
                question = "已知" + eq + "," + "求" + answer_definition + "的共轭复数"
            elif require_answers == ['real+imaginary']:
                question = "已知" + eq + "," + "求" + answer_definition + "的实部与虚部之和"
            elif require_answers == ['modulus']:
                question = "已知" + eq + "," + "求" + answer_definition + "的模"
            elif require_answers == ['quadrant']:
                question = "已知" + eq + "," + "求" + answer_definition + "对应的点位于第( )象限"
            elif require_answers == ['coordinate']:
                question = "已知" + eq + "," + "求" + answer_definition + "对应的点坐标"
            else:
                flag = -1
        else:
            flag = -1
    elif template_id == 'AHFS0003':
        args = get_expression_ahfs0001(raw)
        poly = args[0].latex
        require_answers = raw['params']['require_answers']
        answer_definitions = raw['params']['answer_definitions']
        if len(answer_definitions) == 1:
            answer_definition = answer_definitions[0]
            if answer_definition == 'conjugate':
                question = "求" + poly + "的共轭复数"
                if require_answers == ['value']:
                    question += "的值"
                elif require_answers == ['real']:
                    question += "的实部"
                elif require_answers == ['imaginary']:
                    question += "的虚部"
                elif require_answers == ['conjugate']:
                    question += "的共轭复数"
                elif require_answers == ['real+imaginary']:
                    question += "的实部与虚部之和"
                elif require_answers == ['modulus']:
                    question += "的模"
                elif require_answers == ['quadrant']:
                    question += "对应的点位于第( )象限"
                elif require_answers == ['coordinate']:
                    question += "对应的点坐标"
                else:
                    flag = -1
        else:
            flag = -1
    elif template_id == 'AHFS0006':
        args = get_expression_ahfs0002(raw)
        eq = args[0].latex
        require_answers = raw['params']['require_answers']
        answer_definitions = raw['params']['answer_definitions']
        question = "已知" + eq + ","
        if len(answer_definitions) == 1:
            answer_definition = answer_definitions[0]
            question += "求" + answer_definition
            if require_answers == ['value']:
                question += "的值"
            elif require_answers == ['real']:
                question += "的实部"
            elif require_answers == ['imaginary']:
                question += "的虚部"
            elif require_answers == ['conjugate']:
                question += "的共轭复数"
            elif require_answers == ['real+imaginary']:
                question += "的实部与虚部之和"
            elif require_answers == ['modulus']:
                question += "的模"
            elif require_answers == ['quadrant']:
                question += "在复平面内对应的点位于第( )象限"
            elif require_answers == ['coordinate']:
                question += "对应的点坐标"
            else:
                flag = -1
        else:
            flag = -1
    elif template_id == 'AHFS0007':
        conditions = raw['params']['condition']
        conditions = ",".join(conditions)
        question = conditions
        answer_definitions = raw['params']['answer_definitions']
        require_answers = raw['params']['require_answers']
        if len(answer_definitions) == 1:
            answer_definition = answer_definitions[0]
            question += ',' + '求' + answer_definition
            if require_answers == ["range"]:
                question += "的取值范围."
            elif require_answers == ['value']:
                question += "的值."
            elif require_answers == ['real']:
                question += "的实部"
            elif require_answers == ['imaginary']:
                question += "的虚部"
            elif require_answers == ['conjugate']:
                question += "的共轭复数"
            elif require_answers == ['real+imaginary']:
                question += "的实部与虚部之和"
            elif require_answers == ['modulus']:
                question += "的模"
            elif require_answers == ['quadrant']:
                question += "对应的点位于第( )象限"
            elif require_answers == ['coordinate']:
                question += "对应的点坐标"
            else:
                flag = -1
        else:
            flag = -1
    elif template_id == 'AHFS0005':
        args = get_expression_ahfs0002(raw)
        eq = args[0].latex
        question = "已知" + eq + ","
        distance_obj = raw['params']['distance_obj']
        answer_definitions = raw['params']['answer_definitions']
        if answer_definitions == ["distance"]:
            question += "求复数" + distance_obj[0] + "在复平面上对应的点与点" + distance_obj[1] + "间的距离为( )"
        else:
            flag = -1
    else:
        flag = -1
    if flag == -1:
        return flag, [], ""
    code, t = auto_parser(question)
    if code != 1:
        return code, [], ""
    steps = []
    answer = ""
    try:
        solution, conditions, questions = engine.solving(t, verbose=True, raw=raw)
        if solution and not isinstance(solution, str):
            for step in solution.steps:
                for a, b in step["steps"]:
                    steps.append(b)
            answer = solution.output[-1].printing()
    except Exception:
        flag = -99
    return flag, steps, answer

